package day0307;


import java.util.Arrays;
import java.util.List;

public class Demo02 {

    public static void main(String[] args) {
        Student stu = new Student("张三", 22);
        Student stu1 = new Student("李四", 21);

        // 不可修改的 List : 不能 添加、删除、替换  List.of  List.copyOf  Arrays.asList()
        List of = List.of(stu, stu1);

        System.out.println(of.size());
        System.out.println(of);
        Student stu2 = new Student("王五", 24);
        // 不能添加
//        of.add(new Student("王五", 24));
        // 不能删除
//        of.remove(stu);
        // 不能替换
//        of.set(0, stu2);

        // 修改元素内容是可以的
        Object o = of.get(0);

        if (o instanceof Student s) {
            s.setName("张三1");
            s.setAge(18);
        }

        System.out.println(of.get(0));

        if (of.get(0) instanceof Student s){
            s.show();
        }
    }
}

class Student implements Comparable {

    private String name;

    private int age;


    public Student(String name, int age) {
        this.name = name;
        this.age = age;
    }

    public String getName() {
        return name;
    }

    public int getAge() {
        return age;
    }

    public void setName(String name) {
        this.name = name;
    }

    public void setAge(int age) {
        this.age = age;
    }

    public void show(){
        System.out.println("姓名：" + this.name + ", 年龄: " + this.age);
    }

    @Override
    public int compareTo(Object o) {
        if (o instanceof Student stu){
            return this.age - stu.age;
        }
        return 0;
    }
}
